define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase_order/index' + location.search,
                    add_url: 'purchase_order/add',
                    edit_url: 'purchase_order/edit',
                    del_url: 'purchase_order/del',
                    multi_url: 'purchase_order/multi',
                    import_url: 'purchase_order/import',
                    table: 'purchase_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_name', title: __('Order_name'), operate: 'LIKE'},
                        {field: 'contract.name', title: __('Contract_name'), operate: 'LIKE'},
                        {field: 'purchase_request_id', title: __('Purchase_request_id')},
                        {field: 'supplier_outbound_no', title: __('Supplier_outbound_no'), operate: 'LIKE'},
                        {field: 'delivery_time', title: __('Delivery_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'party_a_id', title: __('Party_a_id'), operate: 'LIKE'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"transfer":__('Transfer'),"cash":__('Cash'),"honour":__('Honour'),"check":__('Check'),"thirty":__('Thirty'),"sixty":__('Sixty'),"ninety":__('Ninety'),"overninety":__('Overninety')}, formatter: Table.api.formatter.normal},
                        {field: 'purchase_by', title: __('Purchase_by'), operate: 'LIKE'},
                        {field: 'has_invoice', title: __('Has_invoice')},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'tax_rate', title: __('Tax_rate'), operate:'BETWEEN'},
                        {field: 'tax_amount', title: __('Tax_amount'), operate:'BETWEEN'},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'uni_price', title: __('Uni_price')},
                        {field: 'tax_uni_price', title: __('Tax_uni_price')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                // 绑定表单事件
                $("#c-uni_price").on('change',function(){
                    var uni_price = parseFloat($(this).val());
                    var tax_rate = parseFloat($("#c-tax_rate").val()) || 0;
                    var tax_uni_price = uni_price * (1 + tax_rate / 100);
                    $("#c-tax_uni_price").val(tax_uni_price.toFixed(2));
                    $("#c-tax_amount").val((tax_uni_price - uni_price).toFixed(2));
                });

                $("#c-tax_rate").on('change',function(){
                    var tax_rate = parseFloat($(this).val()) || 0;
                    var uni_price = parseFloat($("#c-uni_price").val());
                    var tax_uni_price = uni_price * (1 + tax_rate / 100);
                    $("#c-tax_uni_price").val(tax_uni_price.toFixed(2));
                    $("#c-tax_amount").val((tax_uni_price - uni_price).toFixed(2));
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
